'''
Created on Apr 25, 2012

@author: root
'''
#from Role import Role
#from Node import Node
#from Disk import Disk
#from User import User
#from Log import Log
from Log_NodeCheck import Log_NodeCheck
import ssh
import threading
import Queue

class NodeCheck:
    
    Nodes = []
    Log = []
    Roles = []
    Users = []
    returnQueue = Queue.Queue()



    def sshExec(self,cmd,node,callingModule):
        try:
            sshCon = ssh.Connection(node.get_hostName(), username=self.installUser, password=self.installPW)
            (execStatus,execReturn) = sshCon.executeSt(cmd)
        except Exception, err:
            exception = "ERROR: Trouble with SSH Connection to "+node.get_hostName()+" : "+str(err)
            execReturn = exception
            execStatus = 1
            #self.fsLog.exception(exception)
         
        log_NodeCheck = Log_NodeCheck()
        log_NodeCheck.add(node.get_hostName(),callingModule,cmd,execReturn,execStatus)
        self.Log.add_Log_NodeCheck(log_NodeCheck) 
        self.returnQueue.put((node.get_hostName(),execStatus,execReturn))

    def ramCheck(self):
        ramCMD = "sudo cat /proc/meminfo"
        threadCount = 0
        Threads = [] 
        for node in self.Nodes:
            Threads.append(threading.Thread(target=self.sshExec,args=(ramCMD,node, "NodeCheck.ramCheck")))
            Threads[threadCount].daemon=True
            Threads[threadCount].start()     
            threadCount = threadCount + 1
        for thread in Threads:
            thread.join()
            (hostName,status,results) = self.returnQueue.get()
            ramValue = long((str(results[0]).split(":")[1][:-3]).strip())/1024
            if (status == 0):
                for node in self.Nodes:
                    if (node.get_hostName() == hostName):
                        node.set_ramInfo(ramValue)
    
    def cpuCheck(self):
        cpuCMD = "sudo cat /proc/cpuinfo | egrep \"processor|model name\""
     
        threadCount = 0
        Threads = [] 
        for node in self.Nodes:
            Threads.append(threading.Thread(target=self.sshExec,args=(cpuCMD,node, "NodeCheck.cpuCheck")))
            Threads[threadCount].daemon=True
            Threads[threadCount].start()     
            threadCount = threadCount + 1
        for thread in Threads:
            thread.join()
            (hostName,status,results) = self.returnQueue.get()
            cpuCount = str(results).count("processor")
            cpuType = str(results[1]).split(":")[1]
            if (status == 0):
                for node in self.Nodes:
                    if (node.get_hostName() == hostName):
                        node.set_cpuCount(cpuCount)
                        node.set_cpuType(cpuType)
    
    
    
    def rawCapacity(self):
     
        threadCount = 0
        Threads = [] 
            
        for node in self.Nodes:
            for disk in node.get_Disks():
                capCMD = "sudo cat /proc/partitions | grep " + str(disk.get_deviceName())[5:]
                Threads.append(threading.Thread(target=self.sshExec,args=(capCMD,node, "NodeCheck.rawCapacity")))
                Threads[threadCount].daemon=True
                Threads[threadCount].start()     
                threadCount = threadCount + 1
        for thread in Threads:
            thread.join()
            (hostName,status,results) = self.returnQueue.get()
            capacity = str(results).split()[3]
            device = str(results).split()[4][:-4]
            for node in self.Nodes:
                    if (node.get_hostName() == hostName):
                        for disk in node.get_Disks():
                            if (device in disk.get_deviceName()):
                                disk.set_rawCapacity(capacity)
                                
    def set_total_rawCapacity(self):
        for node in self.Nodes:
            raw_Capacity = 0 
            for disk in node.get_Disks():
                raw_Capacity = raw_Capacity + long(disk.get_rawCapacity())
            node.set_total_rawCapacity(raw_Capacity/1024/1024)
        
    
        
    def inventory(self):
        self.ramCheck()
        self.cpuCheck()
        self.rawCapacity()
        self.set_total_rawCapacity()
        

    def set_Users(self):
        for user in self.Users:
            if user.get_role() == "r":
                self.installUser = user.get_userName()
                self.installPW = user.get_password()
            elif user.get_role() == "m":
                self.mapredUser = user.get_userName()
                self.mapredPW = user.get_password()
            elif user.get_role() == "h":
                self.hdfsUser = user.get_userName()
                self.hdfsPW = user.get_password() 


    def __init__(self,Log,Nodes,Users):
        self.Log = Log
        self.Users = Users
        self.Nodes = Nodes
        self.set_Users()
